package com.invokestatic.shooter.Level;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.invokestatic.shooter.Art;
import com.invokestatic.shooter.Input;
import com.invokestatic.shooter.Entity.Entity;
import com.invokestatic.shooter.Entity.Player;
import com.invokestatic.shooter.Screens.GameScreen;
import com.invokestatic.shooter.Screens.Screen;

public class Level {

	public static final double friction = 0.99;
	public List<Entity> entities = new ArrayList<Entity>();
	public final GameScreen screen;
	public final int width, height;
	public byte[][] walls;
	public static int tick = 0;
	public static int bFlagX, bFlagY;
	public static int rFlagX, rFlagY;
	public static ArrayList<Integer> bSpawnX, bSpawnY, rSpawnX, rSpawnY;
	public static Player player;
	public static boolean debug = false;
	
	public World world = new World(new Vector2(0, 0), true); ;
	public Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();

	public Level(GameScreen screen, int w, int h) {
		
		
		this.screen = screen;
		this.width = w;
		this.height = h;
		walls = new byte[w][h];
		bSpawnX = new ArrayList<Integer>();
		bSpawnY = new ArrayList<Integer>();
		rSpawnX = new ArrayList<Integer>();
		rSpawnY = new ArrayList<Integer>();
		for(int y = 0; y < h; y++) {
			for(int x = 0; x < w; x++) {
				int col = (Art.maps_ctf[0].getPixel(x, y) & 0xffffff00) >>> 8;
				if(col == 0xffffff) {
					walls[x][y] = -1;
				}
				if(col == 0x000000) {
					walls[x][y] = 0;
				}
				if(col == 0xFF0000) {
					walls[x][y] = 1;
					rFlagX = x;
					rFlagY = y;
				}
				if(col == 0x0000FF) {
					walls[x][y] = 1;
					bFlagX = x;
					bFlagY = y;
				}
				if(col == 0x00ff00) {
					walls[x][y] = 1;
					bSpawnX.add((x * 64) + 16);
					bSpawnY.add((y * 64) + 16);
				}
				if(col == 0xffff00) {
					walls[x][y] = 1;
					rSpawnX.add(x * 64);
					rSpawnY.add(y * 64);
				}
			}
		}
		
		for(int y = 0; y < h; y++) {
			for(int x = 0; x < w; x++) {
				System.out.print((char) ('B' - walls[x][y]));
			}
			System.out.println();
		}
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DynamicBody; 
		bodyDef.position.set(this.bSpawnX.get(0) + 12, this.bSpawnY.get(0)+12);
		
		Body body = world.createBody(bodyDef); 
		CircleShape circle = new CircleShape();
		circle.setRadius(24f); 
		
		FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = circle; fixtureDef.density = 0.5f;  fixtureDef.friction = 0.4f; fixtureDef.restitution = 0.6f;
		
		Fixture fixture = body.createFixture(fixtureDef);
		
		player = new Player(this.bSpawnX.get(0), this.bSpawnY.get(0));
		add(player);
	}

	public void add (Entity e) {
		entities.add(e);
		e.init(this);
	}

	public void tick(Input input) {
		tick++;
		world.setGravity(new Vector2(0, tick % 360));
		for (int i = entities.size() - 1; i >= 0; i--) {
			Entity e = entities.get(i);
			e.tick(input);
		}
		if(input.buttons[Input.DEBUG] && !input.oldButtons[Input.DEBUG]) {
			debug = !debug;
		}
		world.step(1/60f, 6, 2);
	} 

	Matrix4 matrix = new Matrix4();

	public void render(Screen screen) {
		matrix.idt();
		matrix.setToTranslation(0, 0, 0);
		screen.spriteBatch.setTransformMatrix(matrix);
		int sx = (int)(player.x / (64));
		int sy = (int)(player.y / (64));
		for(int i = 0; i < this.width; i++) {
			for(int j = 0; j < this.height; j++) {
				if(this.walls[i][j] == 0) {
					screen.draw(Art.walls[0][0], (i * 64) - Camera.xO, (j * 64) - Camera.yO, 8, 8);
					//					screen.draw(region, x, y, sx, sy)
				}
			}
		}


		for (int i = entities.size() - 1; i >= 0; i--) {
			Entity e = entities.get(i);
			e.render(screen, null);
		}
		if(debug) {
			Color original = screen.spriteBatch.getColor();
			Color newColor = Color.BLUE;
			screen.spriteBatch.setColor(newColor);
			screen.drawString("DEBUG ENABLED", 0,16);
//			screen.drawStringCentered("("+(int)player.x+","+(int)player.y+")", player.xp - 4, player.yp - 20);
			screen.spriteBatch.setColor(original);
			debugRenderer.render(world, screen.spriteBatch.getProjectionMatrix());
		}
		//		
		//		screen.spriteBatch.end();
	}

}
